<?php
/*
Plugin Name: GCal Events
Description: GCal Events generates a list of events from a public Google Calendar. You need the calendar ID to make it work.
Version: 0.1
Author: Bryn Truscott
*/

include_once('sidebar_events_display.php');
include_once('gcal_event.php');
include_once('wikirenderer-3.1.5/WikiRenderer.lib.php');


class GcalEvents extends WP_Widget 
{
	function GcalEvents()
	{
		add_action('wp_enqueue_scripts', 'gcal_events_add_styles');
		$widget_ops = array('description' => 'A widget that generates a list of events from a public Google Calendar');
		$this->WP_Widget('gcal-events', 'GCal Events', $widget_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args, EXTR_SKIP);
		//open div
		echo $before_widget; 
		
		//widget title
		$title = apply_filters('widget_title', $instance['title']);		
				
		//google calendar parameters
		$params = array(
			'id' => $instance['calendar'], //calendar ID
			'orderby' => $instance['orderby'],
			'sortorder' => $instance['sortorder'],
			'maxevents' => $instance['maxevents']	
		);
		
		if(!empty($title))
		{
			echo $before_title . $title . $after_title;
		}
		
		if(!empty($params['id']))
		{
			$display = new SidebarEventsDisplay();
			$display->getEvents($params);			
		}
		else
		{
			echo __('You must set the calendar ID to make this widget work');
		}
		//close div
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['calendar'] = strip_tags($new_instance['calendar']);
		$instance['orderby'] = $new_instance['orderby'];
		$instance['sortorder'] = $new_instance['sortorder'];
		$instance['maxevents'] = (int)$new_instance['maxevents'];
		return $instance;
	}
	
	function form($instance)
	{
		//http://codex.wordpress.org/Function_Reference/esc_attr
		$title = esc_attr($instance['title']);
		$calendar = esc_attr($instance['calendar']);
		$orderby = esc_attr($instance['orderby']);
		$sortorder = esc_attr($instance['sortorder']);
		$maxevents = esc_attr($instance['maxevents']);
		
		?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">Title: 
				<input class="widefat" 
					id="<?php echo $this->get_field_id('title'); ?>"
					name="<?php echo $this->get_field_name('title'); ?>"
					type="text"
					value="<?php echo $title; ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('calendar'); ?>">Calendar ID: 
				<input class="widefat" 
					id="<?php echo $this->get_field_id('calendar'); ?>"
					name="<?php echo $this->get_field_name('calendar'); ?>"
					type="text"
					value="<?php echo $calendar; ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('orderby'); ?>"><?php echo __('Order by'); ?>: </label>		
			<select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>" class="widefat">
				<option <?php if ( $instance['orderby'] == 'lastmodified'  ) echo 'selected="selected"'; ?>>lastmodified</option>
				<option <?php if ( $instance['orderby'] != 'lastmodified' ) echo 'selected="selected"'; ?>>starttime</option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('sortorder'); ?>"><?php echo __('Sort order'); ?>: </label>		
			<select id="<?php echo $this->get_field_id( 'sortorder' ); ?>" name="<?php echo $this->get_field_name( 'sortorder' ); ?>" class="widefat">
				<option <?php if ( $instance['sortorder'] == 'ascending' ) echo 'selected="selected"'; ?>>ascending</option>
				<option <?php if ( $instance['sortorder'] != 'ascending' ) echo 'selected="selected"'; ?>>descending</option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('maxevents'); ?>"><?php echo __('Max events'); ?>: </label>		
			<select id="<?php echo $this->get_field_id( 'maxevents' ); ?>" name="<?php echo $this->get_field_name( 'maxevents' ); ?>" class="widefat">
				<option <?php if ( $instance['maxevents'] == 1 ) echo 'selected="selected"'; ?>>1</option>
				<option <?php if ( $instance['maxevents'] == 3 ) echo 'selected="selected"'; ?>>3</option>
				<option <?php if ( $instance['maxevents'] == 5 ) echo 'selected="selected"'; ?>>5</option>
				<option <?php if ( $instance['maxevents'] == 10 ) echo 'selected="selected"'; ?>>10</option>
				<option <?php if ( $instance['maxevents'] == 15 ) echo 'selected="selected"'; ?>>15</option>
				<option <?php if ( $instance['maxevents'] == 20 ) echo 'selected="selected"'; ?>>20</option>
			</select>
		</p>
		<?php
	}
}
add_action('widgets_init', create_function('', 'return register_widget("gcalEvents");'));

function gcal_events_add_styles() 
{
    wp_enqueue_style( 'gcal-events-style', plugins_url('gcal-events-style.css', __FILE__) );
}

function gcal_check_event_page()
{
	if ($_GET['cal'] != null && $_GET['event'] != null)
	{
		return true;
	}
	
	return false;
}

function gcal_events_title_filter($title) 
{
	if (gcal_check_event_page())
	{
		$event = new GcalEvent ($_GET['cal'], $_GET['event']);
		
		if ($event->get())
		{
			return $event->title();
		}
		else
		{
			return "Event not found";
		}
	}
	
	return $title;
}

function gcal_events_content_filter($content) 
{
	if (gcal_check_event_page())
	{
		$event = new GcalEvent ($_GET['cal'], $_GET['event']);
		
		if ($event->get())
		{
			$date = $event->date()->format ('l j F Y');
			$startTime = $event->startTime()->format ('g:i a');
			$endTime = $event->endTime()->format ('g:i a');
			$where = $event->where();
			
			$address = $event->address();
			
			if (strlen ($address) > 0)
			{
				$where = $address;
			}
			
//			$content = $event->content();
			$postcode = $event->postcode();

			if (strlen ($postcode) > 0)
			{
				$mapURL = "http://maps.google.co.uk/maps?q=$postcode";
				
				$where .= ' (<a target="_blank" href="' . $mapURL . '">Map</a>)';
			}
			
			 $wikiRenderer = new WikiRenderer();
			 $description = $wikiRenderer->render($event->description());	
					
        	return <<< EOD
<p>
<strong>When:</strong> $date @ $startTime - $endTime<br/>
<strong>Where:</strong> $where
</p>
$description
EOD;
		}
		else
		{
			return "Event not found";
		}
	
	}
	
	return $content;
}

add_filter( 'the_content', 'gcal_events_content_filter' );
add_filter( 'the_title', 'gcal_events_title_filter' );
